<?php

App::uses('AppController', 'Controller');

/**
 * Groups Controller
 *
 * @property Group $Group
 */
class TestsController  extends AppController {

    function beforeFilter() {
        $this->Auth->allow('index','end','reached','bug');	 
        parent::beforeFilter();        
       
    }    
	public function bug(){
		$this->loadModel('QuestionAnswer');
		 $condition= array(
			'QuestionAnswer.name_member'=>$this->Session->read('nameMember'),
			'QuestionAnswer.munber'=>$this->Session->read('locationMember'),
							);
		if($this->QuestionAnswer->deleteAll($condition,false)){
			$this->redirect('index');
		}else{
			$this->redirect('reached');
		}
	}
    public function index() {
        $this->layout = "test"; 
        if($this->Session->read('logTest')!=1){
            $this->redirect("/quiz/confrim");
        }
        //neu ket thuc hoi nghi chuyen den page cam on 
       
      //  if($this->isCheckEnd()>0){
       //     $this->redirect('end');
       // }else{
        //neu submit from
        if ($this->request->is('post')) {
          //khoi tao cau tra loi 
			  $this->loadModel('QuestionAnswer');
			  $checkInsert = $this->QuestionAnswer->find('all',array(
					'conditions'=>array(
						'QuestionAnswer.question_id'=>$_POST['idQuest'],
						'QuestionAnswer.name_member'=>$this->Session->read('nameMember'),
						'QuestionAnswer.munber'=>$this->Session->read('locationMember'),
					)
			  ));
			
			  if(count($checkInsert)==0){ 
				$this->loadModel('Answer');          
				$inforAn =  $this->Answer->read(null,$_POST['ruleAn']);
				$start=strtotime(date($this->Session->read('startTest')));
				$nowTime = strtotime(date("Y-m-d H:i:s"));         
				$between = $nowTime - $start;
				$data = array('QuestionAnswer'=>array(
									  'name_member'=> $this->Session->read('nameMember'),
									  'question_id'=>$inforAn['Answer']['id_quyestion'],
									  'answer_id'=>$inforAn['Answer']['id'],
									  'ruler'=>$inforAn['Answer']['ruler'],
									  'munber'=>$this->Session->read('locationMember'),
									  'total_time'=>$between,
									  'question_end'=>$_POST['end']
							  ));
				if($inforAn['Answer']['ruler']==1){
					$this->QuestionAnswer->save($data);
				}	
			  } 
			if($_POST['end']==1){
				$this->loadModel('QuestionAnswer');
				$ck = $this->QuestionAnswer->find('all',array(
						'conditions'=>array(						
							'QuestionAnswer.name_member'=>$this->Session->read('nameMember'),
							'QuestionAnswer.munber'=>$this->Session->read('locationMember'),
						)
					));
				if(count($ck) < 6){
					//delet cac cau tra loi trc
					$this->redirect('reached');
				}else{	
					$this->redirect('end');
				}
			}else{
				$fullData = $this->getQuestion($_POST['nextOder']);
				$time =45;
				$this->Session->write('startTest',date("Y-m-d H:i:s"));
				$this->Session->write('refer',1);
				$this->Session->write('nextQuest',$_POST['nextOder']);
			}       
			  //end when mothed POST
        } else{
        if($this->Session->read('refer')!=1){
            $this->Session->write('refer',1);
            $this->Session->write('nextQuest',1);
            $fullData = $this->getQuestion(1);
            $time =45;
        }else{
            $start=strtotime(date($this->Session->read('startTest')));
            $nowTime = strtotime(date("Y-m-d H:i:s"));         
            $between = $nowTime - $start;
            if($between < 45){
                //khi F5 chua het thoi gian               
                 $time  = 45-$between;
                 if($this->Session->read('orderNextF5')){
                     $order = $this->Session->read('orderNextF5');
                 }else{
                     $order = 1;
                 }
                
                 $this->Session->write('nextQuest',$order);
                 $fullData = $this->getQuestion($order);
                
            }else{                
                //toi cau tiep theo                
                $order = $this->Session->read('nextQuest')+1;                 
                $fullData = $this->getQuestion($order);
                if($fullData['Question']['end']==1){
                   $this->Session->write('logTest',2); 
                 }else{
                     $this->Session->write('nextQuest',$order);
                 }
                 
                 //$this->Session->delete('refer');
                 $this->Session->write('startTest',date("Y-m-d H:i:s"));
                 $this->Session->write('orderNextF5',$order);
                 $time =45;
               
            }
        }  
          
           
        }   
        $this->set('timeTesst',$time);
        $this->set('data',$fullData);
       // }
        $this->render('/Fontend/Tests/index');
        
    }
   /* public function isInsert(){
        //$this->redirect('index');
    }
    public function checkInsert($id= null){
        $this->loadModel('QuestionAnswer');
        $checkInsert = $this->QuestionAnswer->find('all',array(
                'conditions'=>array(
                    'QuestionAnswer.question_id'=>$id,
                    'QuestionAnswer.name_member'=>$this->Session->read('nameMember'),
                    'QuestionAnswer.munber'=>$this->Session->read('locationMember'),
                )
        ));
       $count = count($checkInsert);
        return $count;
    }*/
	public function reached() {
		$this->Session->delete('refer');
		 $this->Session->write('startTest', date('Y-m-d H:i:s') );
         $this->Session->write('endTest', date('Y-m-d H:i:s') );
		$this->loadModel('QuestionAnswer');
			$ck = $this->QuestionAnswer->find('all',array(
					'conditions'=>array(						
						'QuestionAnswer.name_member'=>$this->Session->read('nameMember'),
						'QuestionAnswer.munber'=>$this->Session->read('locationMember'),
					)
				));
		$this->set('total',count($ck));		
		$this->render('/Fontend/Tests/reached'); 
	}

    public function getQuestion($order=null){
        $this->loadModel("Question");
        $data = $this->Question->find('first',array(
            'conditions'=>array(
                'Question.order'=>$order
            ),
            'fields'=>array(
                'Question.id',
                'Question.name',
                'Question.order',
                'Question.question_end'
            )
        ));       
        $datafull =array(
            'Question'=>array(
                'id'=>$data['Question']['id'],
                'name'=>$data['Question']['name'],
                'order'=>$data['Question']['order'],
                'end'=>$data['Question']['question_end'],
            ),
            //'Orther'=>  $this->getInforOrther(),
            'Answer'=>$this->getAllAnsewByQuestion($data['Question']['id']),
           
            );
        return $datafull;
    }
    public function getAllAnsewByQuestion($id=null){
        $this->loadModel('Answer');
        $data = $this->Answer->find('all',array(
            'conditions'=>array(
                'Answer.id_quyestion'=>$id
            ),
            'fields'=>array('Answer.id','Answer.name'),
            'order'=>'Answer.id'
        ));
        return $data;
    }
    public function getInforOrther($id=null){
        $data = array(
            'name'=>'HỒ PHẠM THỤC LAN',
            'usemame'=>'NSAIDS TRÊN CÁC BỆNH LÝ XƯƠNG KHỚP TRONG THỰC HÀNH LÀM SÀNG',
            'bg'=>1
            );
        return $data;
    }
    public function isCheckEnd(){
        $this->loadModel('QuestionAnswer');     
        $data=$this->QuestionAnswer->find('all',array('conditions'=>array('QuestionAnswer.question_end'=>1,'QuestionAnswer.name_member'=>$this->Session->read('nameMember'))));
        $count = count($data);               
        return $count;
    }
    public function end() {
        $this->render('/Fontend/Tests/end');        
    }
}
?>